$(function(){
    //当前城市
    var currentCity=SYS_GLOBAL_CONFIG.getCurrentCity("currentCity");
     //显示服务，商品，店铺菜单选择
    $(".search-center .center-span,.search-center .seacher-img").on("click",function(){
        $("#classify").toggle(100); 
    });
    var l_s;
    if(window.localStorage){
        l_s=window.localStorage;      
    }else{
        l_s={};
    }
    //条状到不同的搜索页面
    $(".c-ul li").on("click",function(){
        var link=$(this).find("a").attr("data-link");
        var flag_value;
        if($("#centerHistory").css("display")=="block"){
            l_s.search_state=1;
        }else{
            l_s.search_state=0;
        }
        location.href="./"+link+".html?currentCity="+currentCity;
    });
    //获取当前城市名
    var url=SYS_GLOBAL_CONFIG.reqUrl,
   
    
    async="true",
    type="post";
    var areaId="";
    var address=currentCity;//当前城市名
    var nextData;
    var preData;
    var pdTypeId=0;//当前产品的父级id
    var productTypeId=0;//当前的产品id
    var userId="";//当前用户id
    var searchValue="";//搜索内容
    var areas=""||address;//当前街道
    var currentPage=1;//默认设置当前页为第一页
    var totalPage=1;//设置默认总页数
    var show_count=5;//设置默认每次加载的条数
    var productName="";//产品名
    var dataProduct=JSON.stringify({
        "code":"getProductList",
        "version":"1.0",
        "data":{
            "userId":userId,
            "areaId":areaId,
            "currentPage":currentPage,
            "address":currentCity,
            "productTypeId":productTypeId,
            "showCount":show_count,
            "parentPdTypeId":pdTypeId,
            "productName":productName
        }
    });
    function getData(url,requestData,call_func,async,type){
        console.log("请求参数"+requestData);
            $.ajax({
                url:url,
                type:type,
                datatype:"json",
                contentType:"application/json",
                data:requestData,
                async:async,
                success:function(data){
                    call_func(data);  
                },
                error:function(msg){
                    console.log("异常："+msg);
                }   
            });        
        }
    /**
     *加载商品信息
     **/
    function call_func1(data){
        console.log(data);
        var datas=data.data.datas;
        var lens=datas.length;
        totalPage=data.data.totalPage;//总页数
        currentPage=data.data.currentPage//当前页数
        var dataStr1="";
        var dataStr2="";
        var arr1=[];
        var arr2=[];
        for(var i=0;i<lens;i++){
            if(datas[i].sellerFees.length>0){
                if(datas[i].sellerFees[0].isAvaliable==1){
                    arr2.push(datas[i]);    
                }else{
                    arr1.push(datas[i]);
                }
            }else{
                arr1.push(datas[i]);
            }
        }
        console.log(arr1);
        console.log(arr2);
        if(arr1.length>0){
            for(var i=0,len=arr1.length;i<len;i++){
               var isauth="";
               if(arr1[i].seller.isSellerAuth==0){
                   isauth='<span>(未认证)</span>';
               }else{
                   isauth='<img src="./images/vip_icon.png" alt="">';
               }
               dataStr1+='<div data-proId="'+arr1[i].productId+'" class="zwl-noCertificate"><div class="goodNoCertificate"><img class="zwl-img" src="'+arr1[i].mainImgPath+'" alt=""><div class="zwl-shoppingDetail"><p class="zwl-p">'+arr1[i].productName+'</p><span class="shop1">'+arr1[i].productTypeName+'</span><span class="shopRe1">'+isauth+'</span><span class="span2"> <span class="money">'+arr1[i].saleprice+'元/</span><span class="cess">'+arr1[i].unit+'</span></span></div></div></div>';

           }
       }
        var downS='';
        if(arr2.length>0){
            for(var i=0,len=arr2.length;i<len;i++){
               var isauth="";
               if(arr2[i].seller.isSellerAuth==0){
                   isauth='<span>(未认证)</span>';
               }else{
                   isauth='<img src="./images/vip_icon.png" alt="">';
               }
                var main_img='';
                var k_arr=arr2[i].productImgs;
                var k_len=k_arr.length;
                
                if(k_len>0){
                    for(var k=0;k<k_len;k++){
                        if(k===3){
                            break;    
                        }
                        main_img+='<img src="'+k_arr[k].FULLPATH+'" alt="">';    
                    }    
                }
                var t_len=arr2[i].tagServices.length;
                var arrTag=arr2[i].tagServices;
                
                if(t_len>2){
                    downS='<i class="down"></i>';
                }
                var tags='';
                var tagStr='';
                for(var t=0;t<t_len;t++){
                    tagStr+='<p class="tags_info"><img src="'+arrTag[t].tagIconPath+'" alt=""><span>'+arrTag[t].userRemark+'</span></p>';
                    if(t<3){
                        tags+='<span>'+arrTag[t].name+'</span>';    
                    }
                }
                dataStr2+='<div class="zwl-Certificate"><div class="zwl-show" data-proId="'+arr2[i].productId+'"><div class="goodNoCertificate"><img class="zwl-img" src="'+arr2[i].mainImgPath+'" alt=""><div class="zwl-shoppingDetail"><p class="zwl-p">'+arr2[i].productName+'</p><p class="zwl-way">'+tags+'</p><span class="shop">'+arr2[i].productTypeName+'</span><span class="shopRe">'+isauth+'</span><span class="span1"><span class="money">'+arr2[i].saleprice+'元/</span><span class="cess">'+arr2[i].unit+'</span></span></div></div><div class="zwl-pic">'+main_img+'<img src="'+arr2[i].mainImgPath+'" alt=""></div></div><div class="zwl-benefit max2_h">'+tagStr+downS+'</div></div>';
           }
       }
        $("#goods-lists").append(dataStr1+""+dataStr2);
        var down_flag=1;
        if(downS!=''){
            $(".zwl-benefit i").off("click");
            $(".zwl-benefit i").on("click",function(){
                if(down_flag==1){ 
                    $(this).addClass("up").removeClass("down").parent(".zwl-benefit").addClass("auto_h").removeClass("max2_h");
                    down_flag=0;
                }else{
                    $(this).addClass("down").removeClass("up").parent(".zwl-benefit").addClass("max2_h").removeClass("auto_h");
                    down_flag=1;
                }
            });    
        }
        $(".zwl-noCertificate,.zwl-show").on("click",function(){
            var proId=$(this).attr("data-proId");
            location.href="./goodDetail.html?productId="+proId;
        });
   }
    //获取附近产品信息
    getData(url,dataProduct,call_func1,async,type);
    
    var defaultColor="#333";
    var changeColor="#36c7b7";
    var htmlSize=parseInt($("html").css("fontSize"));
    var h=parseInt($(".searchGoodsBody").css("height"));
    var bottomh=2.5*htmlSize;
    /**
     *阴影控制
     **/
    function shadow(){
        var top=$(this).scrollTop();
        if(top>5*htmlSize){
            $(".shadow-top3725").addClass("topAfter").removeClass("topBefore");
        }else{
            $(".shadow-top3725").removeClass("topAfter").addClass("topBefore");
        }
    }
    $(document).on("scroll",$(this),shadow);
    $(".shadow-top3725").on("click",function(){
        $(this).hide();
        doScroll();
        for(var i=0,len=$(".goodsSelect").length;i<len;i++){
            if($(".goodsSelect")[i].style.display!="none"){
                $(".goodsSelect")[i].style.display="none";
            }
        }
        $(".good-nav-li").find("i").attr("class","arrow-bottom");
        $(".good-nav-li").css("color",defaultColor);
    });
    
    //阻止滚动
    var unScroll=function(){
        $('html,body').css({"height":"100%","overflow":"hidden"});      
    }
    //允许滚动
    var doScroll=function(){
        $('html,body').css({"height":"auto","overflow":"auto"});
    }
    //设置页面滚动
    function setScroll(ele){
        console.log(ele);
        console.log("aa11"+$(ele).css("display"));
        if($(ele).css("display")=="block"){
            unScroll();//阻止滚动
        }else{
            doScroll();//允许滚动
        }
    }
    //点击显示地区s
    $("#area").on("click",function(){
        
        var classname=$(this).find("i").attr("class");
        console.log(classname);
        var dataCity=JSON.stringify({
            "code":"getDictAreaList",
            "version":"1.0",
            "data":{
                "level":"2",
                "fullName":address
            }
        });
        console.log(classname.indexOf("arrow-bottom")>-1);
        if(classname.indexOf("arrow-bottom")>-1){
            
            getData(url,dataCity,call_func2,async,type);
        }
        $("#goodArea").slideToggle(100,function(){
            setScroll("#goodArea");
        }).siblings(".goodsSelect").hide();
        
        handle($(this));
        
    });
    /**
     *点击全城，获取全部市区
     **/
    function call_func2(data){
        console.log(data);
        var datas=data.data.dictAreas;
        var areaLen=datas.length;
        var cityStr='<li data-city="" class="area-li bgf036c">全城</li>';
        for(var i=0;i<areaLen;i++){
            cityStr+='<li data-city="'+datas[i].fullName+'" class="area-li  ">'+datas[i].areaName+'</li>';    
        }
        $(".area-nav").html(cityStr);
        $(".area-nav .area-li").off("click");
        $(".area-nav .area-li").on("click",function(){
            $(this).addClass("bgf036c").siblings(".area-li").removeClass("bgf036c");
            var cityname=$(this).attr("data-city");
            var cityname;
            if(cityname==""){
                curr_city=currentCity; 
                $("#area-nav-item").html('<li data-areaId="" data-area="'+curr_city+'"><span>全城</span></li>');
                $("#area-nav-item li").on("click",function(){
                    areaId=$(this).attr("data-areaId");
                    currentPage=1;
                    var area_data=JSON.stringify({
                        "code":"getProductList",
                        "version":"1.0",
                        "data":{
                            "userId":userId,
                            "areaId":areaId,
                            "currentPage":currentPage,
                            "address":currentCity,
                            "productTypeId":productTypeId,
                            "showCount":show_count,
                            "parentPdTypeId":pdTypeId,
                            "productName":productName
                        }
                    });
                    var city=$(this).find("span").html();//当前街道
                    $("#currentCity").html(city);
                    getData(url,area_data,call_func1,async,type);
                    console.log("----");
                    $("#goodSort").hide();
                    $(".shadow-top3725").hide();
                    $("#area").css("color",defaultColor).find("i")
                .addClass("arrow-bottom").removeClass("arrow-bottom36c").removeClass("arrow-top36c").removeClass("arrow-top");
                    $("#goodArea").slideToggle(100,function(){
                        setScroll("#goodArea");
                    }).siblings(".goodsSelect").hide(); 
                    $("#sort").css("color",defaultColor).find("i").attr("class","arrow-bottom");
            });   
            }else{
                curr_city=cityname;   
                var dataArea=JSON.stringify({
                    "code":"getDictAreaList",
                    "version":"1.0",
                    "data":{
                        "level":"3",
                        "fullName":curr_city
                    }
                });   
                getData(url,dataArea,call_func_area,async,type);
            }
        });
    }
    /**
     *获取全部产品
     **/
    function call_func3(data){
        console.log(data);
        var dataType=data.data.productTypes;
        var d_len=dataType.length;
        var  typeStr='<li data-typeId="0" class="sort-li bgf036c">全部分类</li>';
        for(var i=0;i<d_len;i++){
            typeStr+='<li data-typeId="'+dataType[i].pdTypeId+'" class="sort-li topborf0f">'+dataType[i].pdTypeName+'</li>';
        }
        $(".sort-nav").html(typeStr);
        //点击不同的类型加载不同的数据
        $(".sort-nav .sort-li").on("click",function(){
             $(this).addClass("bgf036c").siblings(".sort-li").removeClass("bgf036c");
            pdTypeId=parseInt($(this).attr("data-typeId"));
            var sort_data=JSON.stringify({
                "code":"getPdOrServTypeList",
                "version":"1.0",
                "data":{
                    "parentServiceId":0,
                    "type":"PRODUCT",
                    "parentPdTypeId":pdTypeId
                }
            });
            if(pdTypeId==0){
                $("#sort-nav-item").html('<li data-typeId="0"  class="sort-li bgf036c">全部分类</li>');
                $("#sort-nav-item li").on("click",function(){
                    currentPage=1;
                    productTypeId=0;
                    var s_data=JSON.stringify({
                        "code":"getProductList",
                        "version":"1.0",
                        "data":{
                            "userId":userId,
                            "areaId":areaId,
                            "currentPage":currentPage,
                            "address":currentCity,
                            "productTypeId":productTypeId,
                            "showCount":show_count,
                            "parentPdTypeId":pdTypeId,
                            "productName":productName
                        }
                    });
                    current_Page=1;
                    $("#goods-lists").html("");
                    $("#currentSort").html($(this).html());
                    getData(url,s_data,call_func1,async,type);
                    $("#goodArea").hide();
                    $(".shadow-top3725").hide();                                    $("#sort").css("color",defaultColor).find("i").addClass("arrow-bottom").removeClass("arrow-bottom36c").removeClass("arrow-top36c").removeClass("arrow-top");
                    $("#goodSort").slideToggle(100,function(){
                        setScroll("#goodSort");
                    }).siblings(".shopsSelect").hide();
                    $("#area").css("color",defaultColor).find("i").attr("class","arrow-bottom");
                });
          }else{
                getData(url,sort_data,call_func_sort,async,type);
          }
        });
    }                           
    /**
     *获取地区子类
     **/
    function call_func_area(data){
        console.log(data);
        var datali="";
        var datas=data.data.dictAreas;
        var areaLen=datas.length;
        for(var i=0;i<areaLen;i++){
            datali+='<li data-areaId="'+datas[i].areaId+'" data-area="'+datas[i].fullName+'"><span>'+datas[i].areaName+'</span></li>';
        }
        $("#area-nav-item").html(datali);
        $("#area-nav-item li").on("click",function(){
            areaId=$(this).attr("data-areaId");
            currentPage=1;
            var area_data=JSON.stringify({
                "code":"getProductList",
                "version":"1.0",
                "data":{
                    "userId":userId,
                    "areaId":areaId,
                    "currentPage":currentPage,
                    "address":currentCity,
                    "productTypeId":productTypeId,
                    "showCount":show_count,
                    "parentPdTypeId":pdTypeId,
                    "productName":productName
                }
            });
            var city=$(this).find("span").html();//当前街道
            $("#currentCity").html(city);
            $("#goods-lists").html("");
            current_Page=1;
            getData(url,area_data,call_func1,async,type);
            console.log("----");
            $("#goodSort").hide();
            $(".shadow-top3725").hide();
            $("#area").css("color",defaultColor).find("i")
            .addClass("arrow-bottom").removeClass("arrow-bottom36c").removeClass("arrow-top36c").removeClass("arrow-top"); 
                $("#goodArea").slideToggle(100,function(){
                    setScroll("#goodArea");
                }).siblings(".goodsSelect").hide();   
 $("#sort").css("color",defaultColor).find("i").attr("class","arrow-bottom");
        });
    }
    /**
     *获取全部分类子类
     **/
    function call_func_sort(data){
        console.log(data);
        var datali="";
        var datas=data.data.productTypes;
        var areaLen=datas.length;
        for(var i=0;i<areaLen;i++){
            datali+='<li data-typeId="'+datas[i].pdTypeId+'"><span>'+datas[i].pdTypeName+'</span></li>';
        }
        $("#sort-nav-item").html(datali);
        $("#sort-nav-item li").on("click",function(){
            currentPage=1;
            productTypeId=parseInt($(this).attr("data-typeId"));
            var area_data=JSON.stringify({
                "code":"getProductList",
                "version":"1.0",
                "data":{
                    "userId":userId,
                    "areaId":areaId,
                    "currentPage":currentPage,
                    "address":currentCity,
                    "productTypeId":productTypeId,
                    "showCount":show_count,
                    "parentPdTypeId":pdTypeId,
                    "productName":productName
                }
            });
            current_Page=1;//使页面滚动的时候从第一页开始
            $("#currentSort").html($(this).html());
            $("#goods-lists").html("");
            getData(url,area_data,call_func1,async,type);
            $("#goodArea").hide();
            $(".shadow-top3725").hide();
            $("#sort").css("color",defaultColor).find("i").addClass("arrow-bottom").removeClass("arrow-bottom36c").removeClass("arrow-top36c").removeClass("arrow-top");
            $("#goodSort").slideToggle(100,function(){
                setScroll("#goodSort");
            }).siblings(".shopsSelect").hide();
 $("#area").css("color",defaultColor).find("i").attr("class","arrow-bottom");
        });     
    }
    //点击显示排序
    $("#sort").on("click",function(){
        $("#goodSort").slideToggle(100,function(){
            setScroll("#goodSort");
        }).siblings(".goodsSelect").hide();
        var classname=$(this).find("i").attr("class");
        var allProduct=JSON.stringify({
            "code":"getPdOrServTypeList",
            "version":"1.0",
            "data":{
                "parentServiceId":0,
                "type":"PRODUCT",
                "parentPdTypeId":0
            }
        });
        //获取全部产品
        if(classname.indexOf("arrow-bottom")>-1){
            
            getData(url,allProduct,call_func3,async,type);
        }
        handle($(this));
    });
    
    /**
     *点击显示详细分类或者全城信息  
     **/   
    function handle(a){
        
        var $_this=a;
        $_this.css("color",changeColor);
        if($_this.find("i").attr("class")==="arrow-bottom36c"||$_this.find("i").attr("class")==="arrow-bottom"){
            $_this.find("i").attr("class","arrow-top36c");
            $(".shadow-top3725").show();
        }else{
//                $_this.find("i").attr("class","arrow-bottom36c");
            $(".shadow-top3725").hide();
            $_this.css("color",defaultColor).find("i").attr("class","arrow-bottom");
        }
        $_this.siblings("li").css("color",defaultColor).find("i")
            .addClass("arrow-bottom").removeClass("arrow-bottom36c").removeClass("arrow-top36c").removeClass("arrow-top");
        
    }
    /**
     *点击搜索，获取指定的数据
     **/
    $(".search-right .collect").on("click",function(){
        var val=$("#search-input").val();//搜索内容
        search_func(val);
    });        
    //搜索
    function search_func(val){
        doScroll();//允许滚动
        $("body").css("overflowX","hidden");
        productName=val;
        currentPage=1;
        var sear_data=JSON.stringify({
            "code":"getProductList",
            "version":"1.0",
            "data":{
                "userId":userId,
                "areaId":areaId,
                "currentPage":currentPage,
                "address":currentCity,
                "productTypeId":productTypeId,
                "showCount":show_count,
                "parentPdTypeId":pdTypeId,
                "productName":productName
            }
        });
        var his_data=productName
        localStorage.his_data+=his_data+"/";
        $("#search-input").val("");
        $("#goods-lists").html("");
        current_Page=1;
        //搜索
        getData(url,sear_data,call_func1,async,type);                 
        var t=setTimeout(function(){
            l_s.search_state=0;
            $("#centerHistory").css("display","none");//将搜索隐藏
        },100);  
    }
    console.log(localStorage.his_data);
    if(localStorage.his_data==undefined){
        localStorage.his_data="";       
    }else{
        
    }   
    $("#search-input").on("click",function(){
        unScroll();//阻止滚动
        if(localStorage.his_data.indexOf("/")>-1){
            var h_data_arr=localStorage.his_data.split("/");
            var h_data_str="";
            var len=h_data_arr.length;
            var f=0;//计数器，判断是否超过5
            for(var i=0,len_index=len-1;len_index>=i;len_index--){
                if(f<5){
                    if(h_data_arr[len_index].trim()!=""){
                        h_data_str+='<li><img class="center-img" src="./images/search_icon.png" alt=""><span class="li-span">'+h_data_arr[len_index]+'</span><i></i></li>';
                        f++;
                    }else{
                        h_data_str+='';
                    }
                }else{
                    break;
                }
            }
            console.log(h_data_str);
            $(".history").html(h_data_str);
        }else{
            localStorage.his_data="";
        }
        $(".history li").on("click",function(){
            var val=$(this).find("span").html();
            search_func(val);
        })
    });
    var current_Page=1;
    /**
     *滚动加载更多*
     **/
    $(document).scroll(function(){
        console.log("123");
        $(".data-loading").removeClass("hide").addClass("show");
        console.log((parseFloat($(document).height())-200));
       console.log(parseFloat($(window).height()) + parseFloat($(window).scrollTop())); if((parseFloat($(document).height())-400)==parseFloat($(window).height()) + parseFloat($(window).scrollTop())-400){ 
            console.log(totalPage+"---"+current_Page);
            if(totalPage>current_Page){
                current_Page+=1;
                console.log("执行"+current_Page);
                var data_p=JSON.stringify({
                    "code":"getProductList",
                    "version":"1.0",
                    "data":{
                        "userId":userId,
                        "areaId":areaId,
                        "currentPage":current_Page,
                        "address":currentCity,
                        "productTypeId":productTypeId,
                        "showCount":show_count,
                        "parentPdTypeId":pdTypeId,
                        "productName":productName
                    }
                });
                getData(url,data_p,call_func1,async,type);    
            }else{
                $(".data-loading").html("已加载全部");
            }
        } 
     });
    //清除历史记录
    $(".clear-p").on("click",function(){
        localStorage.his_data="";    
        $(".history").html(localStorage.his_data);
        console.log($(".history").html());
    });
});
